package club.twelveblog.tblog.service;

import club.twelveblog.tblog.dao.BlogInfoDao;
import club.twelveblog.tblog.dao.CommentDao;
import club.twelveblog.tblog.dao.UserDao;
import club.twelveblog.tblog.entity.BlogInfo;
import club.twelveblog.tblog.entity.Comment;
import club.twelveblog.tblog.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Autowired
    private BlogInfoDao blogInfoDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private CommentDao commentDao;
    public void addComment(BlogInfo blogInfo, User user, Comment comment) {
        List<Comment> comments=new ArrayList<>();
        comment.setComment_create_time(new Date());
        comment.setUser(user);
        comment.setBlogInfo(blogInfo);
        comments.add(comment);

        BlogInfo info = blogInfoDao.getOne(blogInfo.getBlog_id());
        info.setComments(comments);
        blogInfoDao.save(info);

        User user1 = userDao.getOne(user.getUser_id());
        user1.setComments(comments);
        userDao.save(user1);
    }

    /**
     * 查找用户的所有评论
     * @param user_id
     * @return
     */
    public List<Comment> findMyComments(Long user_id) {
        List<Comment> comments = commentDao.findCommentsByUid(user_id);
        return comments;
    }
}
